<?php
class Campaigns
{
	const DBTable = "tbl_campaigns";

	public $Campaign_id;	public $Campaign_md5;	public $Title;	public $Description;	public $Client_id;	public $Is_classic;	public $Show_homepage;	public $Show_social;	public $Panel_size;
	public $Profile_image_thumbnail;	public $Profile_image;	public $Meta_title;	public $Meta_description;	public $Meta_keywords;	public $Status;	public $Is_deleted;	public $Createdate;	public $Updatedate;
	public $Url_slug;
	private $__mysql;

	public function __construct()
	{
		$this->Campaign_id = 0;		$this->Campaign_md5 = null;		$this->Title = null;		$this->Description = null;		$this->Client_id = 0;		$this->Is_classic = 0;		$this->Show_homepage = 0;		$this->Show_social = 0;		$this->Panel_size = 0;
		$this->Profile_image_thumbnail = null;		$this->Profile_image = null;		$this->Meta_title = null;		$this->Meta_description = null;		$this->Meta_keywords = null;		$this->Status = 2;		$this->Is_deleted = 0;		$this->Createdate = date("Y-m-d H:i:s");		$this->Updatedate = date("Y-m-d H:i:s");
		$this->Url_slug = null;
		$this->__mysql = new MYSQL;
	}

	public function Save()
	{
		$sqlStr = "insert into ".self::DBTable."
			(title, meta_title, description, client_id, is_classic, show_homepage, show_social, panel_size, profile_image_thumbnail, profile_image, status, createdate, updatedate, url_slug)
			values
			(
			'".mysql_real_escape_string($this->Title)."', 
			'".mysql_real_escape_string($this->Title)."', 			'".mysql_real_escape_string($this->Description)."', 			'".$this->Client_id."', 			'".$this->Is_classic."', 			'".$this->Show_homepage."', 			'".$this->Show_social."', 			'".$this->Panel_size."', 
			'".mysql_real_escape_string($this->Profile_image_thumbnail)."', 			'".mysql_real_escape_string($this->Profile_image)."', 			'".$this->Status."', 			'".$this->Createdate."', 			'".$this->Updatedate."',
			'".clean_url_accents(mysql_real_escape_string($this->Title))."'
			)";

			$this->__mysql->Exec_query($sqlStr);
			$this->Campaign_id = $this->__mysql->Get_insert_id();
			
			//create an md5
			$this->Campaign_md5 = substr(md5($this->Campaign_id), 0, 10);
			$this->Quick_update("campaign_md5", $this->Campaign_md5);
			
			return $this->Campaign_id;
	}

	public function Update()
	{
		//deal with any assets
		$query = "select profile_image, profile_image_thumbnail from ".self::DBTable." where campaign_id = '".mysql_real_escape_string($this->Campaign_id)."'";
		$recordset = $this->__mysql->Exec_query($query);
		$result = $this->__mysql->Get_data_assoc($recordset);

		if($this->Profile_image != "" || $this->Profile_image == "delete")
		{
			if($this->Profile_image == "delete")
			{
				$this->Profile_image = null;
				@unlink(ASSET_STORE_CAMPAIGNS . $result['profile_image']);
			}

			$this->Quick_update("profile_image", $this->Profile_image);
		}

		if($this->Profile_image_thumbnail != "" || $this->Profile_image_thumbnail == "delete")
		{
			if($this->Profile_image_thumbnail == "delete")
			{
				$this->Profile_image_thumbnail = null;
				@unlink(ASSET_STORE_CAMPAIGNS . $result['profile_image_thumbnail']);
			}

			$this->Quick_update("profile_image_thumbnail", $this->Profile_image_thumbnail);
		}
		//
		
		$sqlStr = "update ".self::DBTable." set
			title = '".mysql_real_escape_string($this->Title)."', 			description = '".mysql_real_escape_string($this->Description)."', 			client_id = '".$this->Client_id."', 			is_classic = '".$this->Is_classic."', 			show_homepage = '".$this->Show_homepage."', 			show_social = '".$this->Show_social."', 			panel_size = '".$this->Panel_size."', 
			profile_image_thumbnail = '".mysql_real_escape_string($this->Profile_image_thumbnail)."', 			profile_image = '".mysql_real_escape_string($this->Profile_image)."', 			status = '".$this->Status."', 			updatedate = '".$this->Updatedate."',
			url_slug = '".clean_url_accents(mysql_real_escape_string($this->Url_slug))."'
			where campaign_id = '".mysql_real_escape_string($this->Campaign_id)."'";
			$this->__mysql->Exec_query($sqlStr);
	}

	public function Quick_update($column = null, $value = null)
	{
		$sqlStr = "update ".self::DBTable." set
			".$column." = '".mysql_real_escape_string($value)."',
			 updatedate = '".$this->Updatedate."' 
			where campaign_id = '".mysql_real_escape_string($this->Campaign_id)."'";
			$this->__mysql->Exec_query($sqlStr);
	}

	public function List_all($limit = null, $offset = null)
	{
		$sqlStr = "select cm.*, c.title as client_title
			from ".self::DBTable." cm
			inner join tbl_clients c on c.client_id = cm.client_id
			where cm.is_deleted = 0 
			order by c.title, cm.title";
		if(isset($limit) && isset($offset)){
			$sqlStr .= " limit $offset, $limit";
		}
		//echo $sqlStr;
		$recordset = $this->__mysql->Exec_query($sqlStr);
		return $data_array = $this->__mysql->Get_data_array($recordset);
	}

	public function Homepage_campaigns($limit = null, $offset = null)
	{
		$sqlStr = "select cm.campaign_id, cm.title, c.title as client_title
			from ".self::DBTable." cm
			inner join tbl_clients c on c.client_id = cm.client_id
			where cm.is_deleted = 0 and 
			cm.show_homepage = 1
			order by cm.title";
		if(isset($limit) && isset($offset)){
			$sqlStr .= " limit $offset, $limit";
		}
		//echo $sqlStr;
		$recordset = $this->__mysql->Exec_query($sqlStr);
		return $data_array = $this->__mysql->Get_data_array_assoc($recordset);
	}

	public function List_all_selectlist()
	{
		$sqlStr = "select campaign_id, title
			from ".self::DBTable."
			where is_deleted = 0 
			order by title";
		//echo $sqlStr;
		$recordset = $this->__mysql->Exec_query($sqlStr);
		return $data_array = $this->__mysql->Get_data_array($recordset);
	}

	public function Delete($campaign_id = 0)
	{
		$sqlStr = "update ".self::DBTable." set is_deleted = 1 where campaign_id = '".mysql_real_escape_string($campaign_id)."'";
			$this->__mysql->Exec_query($sqlStr);

		$sqlStr = "optimize table ".self::DBTable."";
			$this->__mysql->Exec_query($sqlStr);
	}

	public function Select($campaign_id = 0)
	{
		$sqlStr = "select * from ".self::DBTable." where campaign_id = '".mysql_real_escape_string($campaign_id)."'";
		$recordset = $this->__mysql->Exec_query($sqlStr);

		if($this->__mysql->Get_num_rows($recordset) > 0)
		{
			$result = $this->__mysql->Get_data_assoc($recordset);
				$this->Campaign_id = $result['campaign_id'];				$this->Campaign_md5 = $result['campaign_md5'];				$this->Title = $result['title'];				$this->Description = $result['description'];				$this->Client_id = $result['client_id'];				$this->Is_classic = $result['is_classic'];				$this->Show_homepage = $result['show_homepage'];				$this->Show_social = $result['show_social'];				$this->Panel_size = $result['panel_size'];
				$this->Profile_image_thumbnail = $result['profile_image_thumbnail'];				$this->Profile_image = $result['profile_image'];				$this->Meta_title = $result['meta_title'];				$this->Meta_description = $result['meta_description'];				$this->Meta_keywords = $result['meta_keywords'];				$this->Status = $result['status'];				$this->Is_deleted = $result['is_deleted'];				$this->Createdate = $result['createdate'];				$this->Updatedate = $result['updatedate'];
				$this->Url_slug = $result['url_slug'];
			return true;
		}
		else
		{
			return false;
		}
	}

	public function Select_by($fieldname = null, $value = null)
	{
		if(!is_null($fieldname))
		{
			$sqlStr = "select * from ".self::DBTable." where ".$fieldname." = '".mysql_real_escape_string($value)."'";
			$recordset = $this->__mysql->Exec_query($sqlStr);

			if($this->__mysql->Get_num_rows($recordset) > 0)
			{
				$result = $this->__mysql->Get_data_assoc($recordset);
				$this->Campaign_id = $result['campaign_id'];				$this->Campaign_md5 = $result['campaign_md5'];				$this->Title = $result['title'];				$this->Description = $result['description'];				$this->Client_id = $result['client_id'];				$this->Is_classic = $result['is_classic'];				$this->Show_homepage = $result['show_homepage'];				$this->Show_social = $result['show_social'];				$this->Panel_size = $result['panel_size'];
				$this->Profile_image_thumbnail = $result['profile_image_thumbnail'];				$this->Profile_image = $result['profile_image'];				$this->Meta_title = $result['meta_title'];				$this->Meta_description = $result['meta_description'];				$this->Meta_keywords = $result['meta_keywords'];				$this->Status = $result['status'];				$this->Is_deleted = $result['is_deleted'];				$this->Createdate = $result['createdate'];				$this->Updatedate = $result['updatedate'];
				$this->Url_slug = $result['url_slug'];
				return true;
			}
			else
			{
				return false;
			}
		}
	}
}
?>